<template>
  <div class="tree-item">
    <span class="i1">{{ item.name }}</span>
    <span class="i2">{{ item.manager }}</span>
    <el-dropdown class="i3" @command="commandEvent">
      <span>
        操作
        <i class="el-icon-arrow-down el-icon--right" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.istop"
            command="look"
          >编辑部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.istop"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDel } from '@/api/departments.js'
export default {
  name: 'TreeItem',
  props: {
    // 当前项的数据
    item: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('show', true, this.item)
          break
        case 'del':
          this.$confirm('你确定要删除此部门吗?', '提示')
            .then(async() => {
              await companyDepartmentDel(this.item.id)
              this.$emit('getData')
              this.$message.success('删除成功')
            })
            .catch(() => {})
          break
        case 'look':
          this.$bus.$emit('show', true, this.item, 'edit')
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-item {
  width: 100%;
  display: flex;
  .i1 {
    flex: 1;
  }
  .i2 {
    width: 150px;
    text-align: right;
  }
  .i3 {
    text-align: center;
    margin-left: 50px;
    width: 100px;
  }
}
</style>
